from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse, redirect,reverse


class MD1(MiddlewareMixin):
    def process_request(self, request):
        # print(request.user.username)
        # print(reverse("ai2109:login"))
        # if request.path == '/ai2109/login/':
        # 统计每个界面访问量
        if request.path == reverse("ai2109:login"):
            pass
        else:

            if not request.user.username:
                return redirect("ai2109:login")
            else:
                print(request.user)
        print('--md1-------process_request-------', id(request))
        # return HttpResponse('你回去吧,此路不通')

    def process_response(self, request, response):
        # 基于请求响应
        print("-----md1-------------process_response--------", id(request))  # 在视图之后
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        print("-----md1------------process_view--------", id(request))  # 在视图之前执行 顺序执行

        # print(view_func.__name__)

    def process_exception(self, request, exception):  # 引发错误 才会触发这个方法
        print("----md1-------process_exception-------", id(request))
        return HttpResponse(exception)  # 返回错误信息

class xxx(MiddlewareMixin):
    pass
